﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;

public partial class adm_user_edit_win : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                if (Request.Params["aid"] != null)
                {
                    this.btnSubmit.Visible = false;
                }
                if (SessionManager.IsLogged)
                {
                    this._Binddropdown();
                    this._Binddata();
                    //this.drpUserType.SelectedIndex = 1;
                }
                else
                {
                    SessionManager.IsLogged = false;
                    string href= "~/login.aspx?ckid=" + SessionManager.GetCookie();
                    Response.Redirect(href);
                }
            }
            catch (Exception ex)
            {
                //throw new error
            }
        }
    }

    private void _Binddropdown()
    {
        //int i=0;
        foreach(a_UserType at in a_UserTypeService.Geta_UserTypeList(a_UserTypeColumns.Name, OrderDirection.ASC))
        {
            if (at.UserTypeID > 1)
            {
              drpUserType.Items.Add(new ListItem(at.Name, at.UserTypeID.ToString()));
                //this.drpUserType.Items.Add(at.Name);
                //this.drpUserType.Items[i-1].Value = at.UserTypeID.ToString();
            }
            //i++;
        }
    }

    private void _Binddata()
    {
        try
        {
            int aid = int.Parse(Request.Params["aid"].ToString());
            a_User au = a_UserService.Geta_User(aid);
            if (au != null)
            {
                this.txtFirstName.Text = au.Firstname;
                this.txtLastname.Text = au.Lastname;
                this.txtLoginEmail.Text = au.Login_Email;
                this.txtPassword.Text = au.Password;
                this.txtConfirmPassword.Text = au.Password;
                this.drpGener.SelectedValue = au.Gender;
                if (au.IsActive == false)
                {
                    this.drpStatus.SelectedValue = "0";
                }
                else
                {
                    this.drpStatus.SelectedValue = "1";
                }
                this.drpUserType.SelectedValue = au.UserTypeID.ToString();
            }
        }
        catch (Exception ex)
        {
            this.txtLoginEmail.Text = "";
            //throw exception
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        a_User au;
        if (this.txtConfirmPassword.Text != this.txtPassword.Text)
        {
            CommonFunc.ShowAlertMessage(this.lblAlert, "Password does not match confirm password");
            return;
        }
        else
        {
            //Kiem tra Login email da ton tai chua?
            au = a_UserService.Geta_UserByLoginEmail(this.txtLoginEmail.Text.Trim());
            if (au == null)
            {
                au = new a_User();
                au.DateOpen = DateTime.Now;
                au.Firstname = this.txtFirstName.Text;
                au.Lastname = this.txtLastname.Text;
                au.LastVisit = DateTime.Now;
                au.Login_Email = this.txtLoginEmail.Text;
                au.Password = this.txtPassword.Text;
                au.UserTypeID = int.Parse(this.drpUserType.SelectedValue);
                au.CookieID = Guid.NewGuid().ToString();
                au.Gender = this.drpGener.SelectedValue;
                au.Title = this.drpGener.SelectedValue;
                au.LastVisit = DateTime.Now;
                if (this.drpStatus.SelectedValue == "0")
                {
                    au.IsActive = false;
                    au.EmailConfirmed = false;
                }
                else
                {
                    au.IsActive = true;
                    au.EmailConfirmed = true;
                }
                a_UserService.Createa_User(au);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Add user sucessfully");
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "Email existed, please input other email");
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int aid = int.Parse(Request.Params["aid"].ToString());
        a_User au = a_UserService.Geta_User(aid);
        if (au != null)
        {
            if (this.txtConfirmPassword.Text != this.txtPassword.Text)
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "Password does not match confirm password");
                return;
            }
            else
            {
                au.DateOpen = DateTime.Now;
                au.Firstname = this.txtFirstName.Text;
                au.Lastname = this.txtLastname.Text;
                au.LastVisit = DateTime.Now;
                au.Login_Email = this.txtLoginEmail.Text;
                au.Password = this.txtPassword.Text;
                au.UserTypeID = int.Parse(this.drpUserType.SelectedValue);
                au.Gender = this.drpGener.SelectedValue;
                au.Title = this.drpGener.SelectedValue;
                au.LastVisit = DateTime.Now;
                if (this.drpStatus.SelectedValue == "0")
                {
                    au.IsActive = false;
                    au.EmailConfirmed = false;
                }
                else
                {
                    au.IsActive = true;
                    au.EmailConfirmed = true;
                }
                a_UserService.Updatea_User(au);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Update user sucessfully");
            }
        }
    }
}
